package article

import (
	"go-gin-api/app/model"
	"go-gin-api/app/util"
	"strconv"

	"github.com/gin-gonic/gin"
)

type wordsRes struct {
	ID    uint   `json:"id"`
	Text  string `json:"text"`
	Value string `json:"value"`
}

// GetWords 获取标签云服务
func GetWords(c *gin.Context) *util.Response {
	words := make([]*model.ArticleType, 0)
	// 查找文章类型
	if err := model.DB.Find(&words).Error; err != nil {
		return &util.Response{
			Code:    4000,
			Message: "failed",
			Data:    "查询类型错误",
		}
	}
	// 查询文章类型对应文章
	result := make([]*wordsRes, 0)
	for _, v := range words {
		count := 0
		// 查询文章数量
		if err := model.DB.Model(&model.Article{}).Where("type_id=?", v.ID).Count(&count).Error; err != nil {
			return &util.Response{
				Code:    4000,
				Message: "failed",
				Data:    "查询类型数量失败!",
			}
		}
		result = append(result, &wordsRes{Value: strconv.Itoa(count), Text: v.TypeName, ID: v.ID})
	}
	return &util.Response{
		Code:    200,
		Message: "success",
		Data:    result,
	}
}
